// pages/index/plugin/search/search.js
Page({
  data: {
    hot_list: ['鲤鱼潭狮', '南狮', '北狮'],
    list: [],
    search: '',
    searchResult: [] 
  },

  onLoad(options) {
    if (wx.getStorageSync('search_history')) {
      this.setData({
        list: JSON.parse(wx.getStorageSync('search_history')).slice(0, 15)
      });
    }
  },

  onShow() {
    
  },

  getData(e) {
    let data = e.detail.value.trim(); 
    if (data !== '') {
      this.searchData(data);
    }
  },

  clear_input() {
    this.setData({
      search: ''
    });
  },

  getSearch(e) {
    let { index } = e.currentTarget.dataset; 
    let va = this.data.hot_list[index]; 
    this.setData({
      search: va 
    });
    this.searchData(va); 
  },
  

  getSearchOne(e) {
    let { index } = e.currentTarget.dataset;
    let va = this.data.list[index];
    this.setData({
      search: va
    });
    this.searchData(va);
  },

  searchData(keyword) {
    this.data.list = this.data.list.filter(item => item !== keyword); 
    this.data.list.unshift(keyword); 
    this.setData({
      list: this.data.list.slice(0, 15), 
      search: keyword 
    });
    wx.setStorageSync('search_history', JSON.stringify(this.data.list)); 


    const db = wx.cloud.database();
    db.collection('feature').where({
      title: db.RegExp({
        regexp: keyword,
        options: 'i' 
      })
    }).get().then(res => {
      this.setData({
        searchResult: res.data 
      });
    }).catch(err => {
      console.error('查询失败：', err);
    });
  },

  clearHistory() {
    this.setData({
      list: []
    });
    wx.removeStorageSync('search_history');
  },

  nextPage:function(e){
    const id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: `../feature/feature?_id=${id}`,
    })
  },



  
});
